﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Tomato.Authentication;
using Tomato.App;

namespace Tomato
{
    public class BaseController : Controller
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="data"></param>
        /// <param name="duration"></param>
        public void CookieSignIn(object data, TimeSpan duration)
        {
            CookieAuthenticator.SignIn(this, data, duration);
        }

        public bool IsCookieSignedIn()
        {
            return CookieAuthenticator.IsSigneIn(this);
        }

        public T GetCookieSignInData<T>()
        {
            return CookieAuthenticator.GetSignInData<T>(this);
        }

        public void CookieSignOff()
        {
            CookieAuthenticator.SignOff(this);
        }


        public void SessionSignIn(object data)
        {
           SessionAuthenticator.SignIn(this, data);
        }

        public bool IsSessionSignedIn()
        {
            return SessionAuthenticator.IsSigneIn(this);
        }

        public T GetSessionSignInData<T>()
        {
            return SessionAuthenticator.GetSignInData<T>(this);
        }

        public void SessionSignOff()
        {
            SessionAuthenticator.SignOff(this);
        }


        /// <summary>
        /// 以JSON形式返回服务模型。一般用于Ajax请求中。
        /// </summary>
        /// <param name="serviceModel">要返回的服务模型</param>
        /// <returns>ServiceModelResult对象</returns>
        public ServiceModelResult ServiceModel(object serviceModel)
        {
            return new ServiceModelResult(serviceModel);
        }

        /// <summary>
        /// 返回错误信息。客户端将接收到错误码为500的错误信息。
        /// </summary>
        /// <param name="message">错误信息</param>
        /// <returns>ErrorResult对象</returns>
        public ErrorResult Error(string message)
        {
            return new ErrorResult() { Message = message };
        }

        /// <summary>
        /// 返回状态码和错误信息。
        /// </summary>
        /// <param name="statusCode">状态码，例如404，500等</param>
        /// <param name="message">错误信息</param>
        /// <returns>ErrorResult对象</returns>
        public ErrorResult Error(int statusCode, string message)
        {
            return new ErrorResult() { StatusCode = statusCode, Message = message };
        }

        /// <summary>
        /// 返回状态码，错误码，和描述。
        /// </summary>
        /// <param name="statusCode">状态码，例如404，500等。</param>
        /// <param name="message">错误信息</param>
        /// <param name="description">错误描述信息，一般是对错误的进一步描述</param>
        /// <returns>ErrorResult对象</returns>
        public ErrorResult Error(int statusCode, string message, string description)
        {
            return new ErrorResult() { StatusCode = statusCode, Message = message, Description = description };
        }

    }
}
